package com.java8feature.java8action.streamTest;

import com.test.stream.User;
import com.test.stream.User2;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/6/13 14:48
 */
public class StreamTest2 {
    public static void main(String[] args) {
        ArrayList<User> arrayList = new ArrayList();
        User user1 = new User();
        user1.setName("user1");
        user1.setId(1);
        user1.setStatus(1);
        user1.setType(2);

        User user4 = new User();
        user4.setName("user4");
        user4.setId(1);
        user4.setStatus(2);
        user4.setType(1);

        User user2 = new User();
        user2.setName("user2");
        user2.setId(1);
        user2.setStatus(2);
        user2.setType(3);


        User user5 = new User();
        user5.setName("user5");
        user5.setId(1);
        user5.setStatus(3);
        user5.setType(3);


        User user3 = new User();
        user3.setName("user3");
        user3.setId(3);
        user3.setStatus(3);
        user3.setType(3);

        arrayList.add(user4);
        arrayList.add(user1);
        arrayList.add(user2);
        arrayList.add(user3);
        arrayList.add(user5);

        List<User2> user2List = arrayList.stream().map(u -> {
            User2 user21 = new User2();
            user21.setId(u.getId());
            return user21;
        } ).collect(Collectors.toList());

        System.out.println(user2List);

        UnaryOperator<String> headerProcessing =
                (String text) -> "Hello World" + text;

        UnaryOperator<String> spellCheckerProcessing =
                (String text) -> text.replaceAll("l", "h");

        String s = null;
        String s1 = Optional.ofNullable(s).orElse("空字符串");
        System.out.println(s1);
//        Optional.ofNullable().get();
//        Optional.ofNullable().filter(

        ForkJoinPool pool = new ForkJoinPool();

       // pool.execute();




      //  Function<String, String> pipeLine = headerProcessing.andThen
    }


    class SumTask extends RecursiveTask<Long> {
        static final int THRESHOLD = 500;
        long[] array;
        int start;
        int end;

        SumTask(long[] array, int start, int end) {
            this.array = array;
            this.start = start;
            this.end = end;
        }

        @Override
        protected Long compute() {
                if (end - start <= THRESHOLD) {
                    // 如果任务足够小,直接计算:
                    long sum = 0;
                    for (int i = start; i < end; i++) {
                        sum += this.array[i];
                        // 故意放慢计算速度:
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                        }
                    }
                    return sum;
                }
                // 任务太大,一分为二:
                int middle = (end + start) / 2;
                System.out.println(String.format("split %d~%d ==> %d~%d, %d~%d", start, end, start, middle, middle, end));
                SumTask subtask1 = new SumTask(this.array, start, middle);
                SumTask subtask2 = new SumTask(this.array, middle, end);
                invokeAll(subtask1, subtask2);
                // 对Join的调用会导致被调用者阻塞。
                Long subresult1 = subtask1.join();
                Long subresult2 = subtask2.join();
                Long result = subresult1 + subresult2;
                System.out.println("result = " + subresult1 + " + " + subresult2 + " ==> " + result);
                return result;
            }
    }
}
